package com.dong.boot.controller.system.basic;

import com.dong.boot.model.Menu;
import com.dong.boot.model.RespBean;
import com.dong.boot.model.Role;
import com.dong.boot.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/roleMenu")
public class RoleMenuController {
    @Autowired
    RoleMenuService roleMenuService;

    @GetMapping("/")
    public List<Menu> getMenusWithChildren(){
        return roleMenuService.getMenusWithChildren();
    }

    @GetMapping("/{id}")
    public List<Integer> getMidsByRid(@PathVariable Integer id){
        return roleMenuService.getMidsByRid(id);
    }

    @PostMapping("/")
    public RespBean updateRoleMenu(Integer rid, Integer[] mids){
        if(roleMenuService.updateRoleMenu(rid,mids)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @DeleteMapping("/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if (roleMenuService.deleteRole(rid)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean addRole(@RequestBody Role role){
        if (roleMenuService.addRole(role)){
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }

}
